<?php


namespace app\admin\controller;


use app\BaseController;
use think\facade\Db;
use think\facade\View;

class Bidding extends BaseController
{
    public function trimall($str){//去除空格
        $oldchar=array(" ","　","\t","\n","\r");
        $newchar=array("","","","","");
        return str_replace($oldchar,$newchar,$str);
    }
    //类目列表
    public function leimu(){
        $info = Db::name('i_category')->where('pid',0)->where('delete',0)->order('id desc')->select()->toArray();
        foreach($info as &$v){
            $v['list'] = Db::name('i_category')->where('pid',$v['id'])->where('delete',0)->order('id desc')->select()->toArray();
        }
        $count = Db::name('i_category')->count();
        View::assign('count',$count);
        View::assign('info',$info);
        return View::fetch();
    }
    //类目列表 --添加页面
    public function leimu_add(){
        $info=Db::name('i_category')->where('pid',0)->select();
        View::assign('info',$info);
        return View::fetch();
    }
//类目列表 --添加数据
    public function leiinsert(){
        $type=input('type');
        $ff=input('data');
        $aa=['pid'=>$ff['pid'],'name'=>$ff['title']];
        $insertss=Db::name('i_category')->insert($aa);
        if ($insertss){
            return json(['status'=>1,'info'=>'添加成功']);
        }
    }
    //类目列表 --修改显示
    public function leimu_update(){
        $id=input('id');
        $aa=Db::name('i_category')->where('id',$id)->find();
        $firsts=Db::name('i_category')->where('pid',0)->select();
        $sc=Db::name('i_category')->select();
        View::assign('id',$id);
        View::assign('firsts',$firsts);
        View::assign('sc',$sc);
        View::assign('ccc',$aa);
        return View::fetch();
    }
    //类目列表 --修改
    public function leimu_up(){
        $ff=input('data');
        if ($ff['pid']==$ff['id']){
            return json(['status'=>3,'infoss'=>'这已经是最顶级了']);
        }else{
            $vv=['pid'=>$ff['pid'],'name'=>$ff['title']];
            $bb=Db::name('i_category')->where('id',$ff['id'])->update($vv);
            if ($bb){
                return json(['status'=>1,'infoss'=>'修改成功']);

            }else{
                return json(['status'=>2,'infoss'=>'修改失败']);
            }
        }

    }
    //类目列表 --删除数据
    public function leidelete(){
        $id=input('id');
        $aa=Db::table('i_category')->where('id',$id)->update(['delete'=>1]);
        if ($aa){
            return json(['status'=>1,'info'=>'删除成功']);
        }else{
            return json(['status'=>2,'info'=>'删除失败']);
        }
    }
    //类目 --禁用
    public function leimu_qiyong(){
        $data=[
            'id'=>input('id'),
            'state'=>input('state')?0:1
        ];
        $info = Db::name('i_category')->update($data);
        if ($info){
            $arr=[
                'code'=>100,
                'msg'=>'操作成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'操作失败'
            ];
        }
        return json($arr);
    }
    //类目未删除页面
    public function leimu_huifu(){
        $info=Db::name('i_category')->where('delete',1)->select()->toArray();
        $aa=count($info);
        View::assign('count',$aa);
        View::assign('info',$info);
        return View::fetch();
    }
    //恢复delete
    public function leimu_huifude(){
        $id=input('id');
        $aaa=input('delete');
        if ($aaa==0){
            $cc=Db::name('i_category')->where('id',$id)->update(['delete'=>1]);
            if ($cc){
                return json(['code'=>100,'msg'=>'恢复成功']);
            }else{
                return json(['code'=>200,'msg'=>'恢复失败']);
            }
        }elseif ($aaa==1){
            $cc=Db::name('i_category')->where('id',$id)->update(['delete'=>0]);
            if ($cc){
                return json(['code'=>100,'msg'=>'恢复成功']);
            }else{
                return json(['code'=>200,'msg'=>'恢复失败']);
            }
        }
    }
    //招标列表
    public function zhaobiao(){
        $user=input('title');
        $use = $this->trimall($user);
        $bid = input('bid');
        $info = Db::name('bidding_hall')
            ->field('b.id,b.number,b.delete,b.title,b.price,i.name as catename,c.name as catepidname,b.state')
            ->alias('b')
            ->join(['i_category'=>'i'],'b.cate=i.id')
            ->join(['i_category'=>'c'],'b.catepid=c.id')
            ->where('b.delete',0)
            ->whereLike('title',"%$use%")
            ->whereLike('bid',"%$bid%")
            ->order('b.id desc')
            ->paginate('4');
        $page = $info->render();
        View::assign('page',$page);
        $aa=count($info);
        View::assign('count',$aa);
        View::assign('info',$info);
        return View::fetch();
    }
    public function zhaobiaoXq(){//招标列表--详情页
        $id = input('id');
        $info = Db::name('bidding_hall')
            ->field('b.id,b.title,i.name catename,c.name catepidname,b.number,b.price,b.time,b.times,b.name,b.phone,b.state,b.describe,b.file')
            ->alias('b')
            ->join(['i_category'=>'i'],'b.cate=i.id')
            ->join(['i_category'=>'c'],'b.catepid=c.id')
            ->where('b.id',$id)
            ->find();
        $formatData_01 = htmlspecialchars_decode($info['describe']);//把一些预定义的 HTML 实体转换为字符
        $formatData_02 = strip_tags($formatData_01);//函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
        $info['describe'] = $formatData_02;
        View::assign('info',$info);
        return View::fetch();
    }
    //招标列表--修改页面
    public function zhaobiao_gai(){
        $id=input('id');
        $info = Db::name('bidding_hall')
            ->field('b.id,b.title,i.name catename,c.name catepidname,b.number,b.price,b.time,b.times,b.name,b.phone,b.state,b.describe,b.file')
            ->alias('b')
            ->join(['i_category'=>'i'],'b.cate=i.id')
            ->join(['i_category'=>'c'],'b.catepid=c.id')
            ->where('b.id',$id)
            ->find();
        $formatData_01 = htmlspecialchars_decode($info['describe']);//把一些预定义的 HTML 实体转换为字符
        $formatData_02 = strip_tags($formatData_01);//函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
        $info['describe'] = $formatData_02;
        View::assign('info',$info);
        return View::fetch();
    }
    //招标列表——修改
    public function zhaobiao_xiugai(){
        $data=input('data');
        $aa=['title'=>$data['title'],'price'=>$data['price'],'name'=>$data['names'],'phone'=>$data['phones'],'describe'=>$data['content']];
        $bb=Db::name('bidding_hall')->where('id',$data['id'])->update($aa);
        if ($bb){
            return json(['status'=>1,'infoss'=>'修改成功']);
        }else{
            return json(['status'=>2,'infoss'=>'修改失败']);
        }
    }
    public function zhaobiaoQY(){//招标列表--启用-禁用
        $data=[
            'id'=>input('id'),

            'state'=>input('state')?0:1
        ];
        $info = Db::name('bidding_hall')->update($data);
        if ($info){
            $arr=[
                'code'=>100,
                'msg'=>'操作成功'
            ];
        }else{
            $arr=[
                'code'=>101,
                'msg'=>'操作失败'
            ];
        }
        return json($arr);
    }
    //招标恢复页面
    public function zhaobiao_huifu(){
        $info = Db::name('bidding_hall')
            ->field('b.id,b.title,i.name catename,c.name catepidname,b.number,b.price,b.time,b.times,b.name,b.phone,b.state,b.describe,b.file,b.delete')
            ->alias('b')
            ->join(['i_category'=>'i'],'b.cate=i.id')
            ->join(['i_category'=>'c'],'b.catepid=c.id')
            ->where('b.delete',1)
            ->select()
            ->toArray();
        $aa=count($info);
        View::assign('count',$aa);
        View::assign('info',$info);
        return View::fetch();
    }
    //招标删除
    public function zhaobiao_delete(){
        $id=input('id');
        $aaa=input('delete');
        if ($aaa==0){
            $aa=Db::name('bidding_hall')->where('id',$id)->update(['delete'=>1]);
            if ($aa){
                return json(['status'=>1,'info'=>'删除成功']);
            }else{
                return json(['status'=>2,'info'=>'删除失败']);
            }
        }elseif($aaa==1){
            $aa=Db::name('bidding_hall')->where('id',$id)->update(['delete'=>0]);
            if ($aa){
                return json(['status'=>1,'info'=>'删除成功']);
            }else{
                return json(['status'=>2,'info'=>'删除失败']);
            }
        }

    }
//招标恢复
    public function zhaobiao_huifude(){
        $id=input('id');
        $aa=input('delete');
        if ($aa==0){
            $cc=Db::name('bidding_hall')->where('id',$id)->update(['delete'=>1]);
            if ($cc){
                return json(['code'=>100,'msg'=>'恢复成功']);
            }else{
                return json(['code'=>200,'msg'=>'恢复失败']);
            }
        }elseif($aa==1){
            $cc=Db::name('bidding_hall')->where('id',$id)->update(['delete'=>0]);
            if ($cc){
                return json(['code'=>100,'msg'=>'恢复成功']);
            }else{
                return json(['code'=>200,'msg'=>'恢复失败']);
            }
        }
    }
}